/**
 * Company
 * Copyright (C) 1992-2020 All Rights Reserved.
 */
package com.yinhai.mycat_test.service.write.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yinhai.mycat_test.VO.InsertOrderVO;
import com.yinhai.mycat_test.entity.Order;
import com.yinhai.mycat_test.entity.OrderItem;
import com.yinhai.mycat_test.mapper.write.OrderItemWriteMapper;
import com.yinhai.mycat_test.mapper.write.OrderWriteMapper;
import com.yinhai.mycat_test.service.BaseService;
import com.yinhai.mycat_test.service.write.OrderWriteService;

/**
 * @author wanggan@yinhai.com
 * @version 1.0
 * @date 2020/4/10 09:31
 * @since JDK1.8
 */
@Service
public class OrderWriteServiceImpl extends BaseService implements OrderWriteService {
    @Autowired
    private OrderWriteMapper orderWriteMapper;
    
    @Autowired
    private OrderItemWriteMapper orderItemWriteMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteOrderListByOrderIdAndUserId(Long userId, List<Long> orderIds) {
        orderItemWriteMapper.deleteOrderItemListByOrderIds(orderIds);
        orderWriteMapper.batchDeleteOrders(orderIds,userId);
        return "success";
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchInsertOrder(List<Order> orderList) {
        orderWriteMapper.batchInsertOrder(orderList);
        return "success batch insert orders";
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String emptyOrder() {
        orderItemWriteMapper.truncateOrderDetail();
        orderWriteMapper.truncateOrder();
        return "success";
    }
    
    @Override
    public String insertOrder(Order order) {
        orderWriteMapper.insertOrder(order);
        return "success";
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteOrderByOrderId(Long userId, Long orderId) {
        orderWriteMapper.deleteOrderByOrderIdAndUserId(orderId,userId);
        orderItemWriteMapper.deleteOrderItemListByOrderId(orderId);
        return "success";
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertOrderByUserId(InsertOrderVO insertOrderVO) {
        if(insertOrderVO.getItemList().size()==0){
            throw new RuntimeException("no order detail");
        }
        Order order = new Order();
        order.setAddress(insertOrderVO.getAddress());
        order.setUserId(insertOrderVO.getUserId());
        order.setOrderTime(getNow());
        Long orderId = getOrderId();
        order.setOrderId(orderId);
        order.setTotalPrice(insertOrderVO.getTotalPrice());
        int res = orderWriteMapper.insertOrder(order);
        if(res > 0 ){
            // 插入订单详情
            List<OrderItem> orderItemList = insertOrderVO.getItemList().stream().map(item -> {
                OrderItem orderItem = new OrderItem();
                orderItem.setGoodsCount(item.getItemCount());
                orderItem.setGoodsName(item.getItemName());
                orderItem.setGoodsId(item.getItemId());
                orderItem.setOrderId(orderId);
                orderItem.setGoodsPrice(item.getItemPrice());
                orderItem.setId(getOrderId());
                return orderItem;
            }).collect(Collectors.toList());
            orderItemWriteMapper.addListOrderItem(orderItemList);
            return "success,orderId="+orderId;
        }
        return "insert error";
    }
}
